package elcs.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import elcs.board.model.BoardService;
import elcs.board.model.BoardVO;
import elcs.board.model.ListVO;

@Controller
public class BoardController {
	@Resource(name = "boardServiceImpl")
	private BoardService boardService;

	@RequestMapping("/view.do")
	public ModelAndView getViewList(BoardVO bvo, HttpServletRequest request)
			throws Exception {
		ModelAndView mv = new ModelAndView();
		
		
		String pageNo = request.getParameter("pageNo");
		
		Map map = new HashMap();
		map.put("head", bvo.getHead());
		map.put("pageNo", pageNo);
		map.put("category", bvo.getCategory());
		String type = request.getParameter("type");
	
		ListVO lvo = boardService.getViewList(map);
		
		
	
		if (type.equals("reply")) {
			if (lvo.getList().isEmpty()) {

				mv.addObject("bvo", bvo);
				mv.setViewName("reply/list.board");
			}else{
				
			mv.addObject("lvo", lvo);
			mv.setViewName("reply/list.board");
			}
		} else if (type.equals("basic")) {
			if (lvo.getList().isEmpty()) {

				mv.addObject("bvo", bvo);
				mv.setViewName("basic/list.board");
			}else{
			mv.addObject("lvo", lvo);
			mv.setViewName("basic/list.board");
			}
		} else if (type.equals("answer")) {
			
			if (lvo.getList().isEmpty()) {

				mv.addObject("bvo", bvo);
				mv.setViewName("answer/list.board");
			}else{
			mv.addObject("lvo", lvo);
			mv.setViewName("answer/list.board");
			}
		}
		return mv;
	}

	@RequestMapping("/board/detail.do")
	public ModelAndView detailView(BoardVO bvo, HttpServletRequest request)
			throws Exception {
		ModelAndView mv = new ModelAndView();
		String type = request.getParameter("type");

		boardService.addCountView(bvo);
		bvo = boardService.detailView(bvo);
	
		if (type.equals("reply")) {
			mv.addObject("bvo", bvo);
			mv.setViewName("reply/detail.board");
		} else if (type.equals("basic")) {
			mv.addObject("bvo", bvo);
			mv.setViewName("basic/detail.board");
		} else if (type.equals("answer")) {
			mv.addObject("bvo", bvo);
			mv.setViewName("answer/detail.board");
		}

		return mv;
	}

	@RequestMapping("/board/delete.do")
	public ModelAndView deleteBoard(BoardVO bvo, HttpServletRequest request)
			throws Exception {
		ModelAndView mv = new ModelAndView();
		String type = request.getParameter("type");

		boardService.deleteBoard(bvo);

		if (type.equals("reply")) {

			mv.setViewName("redirect:/board/view.do?head=" + bvo.getHead()
					+ "&type=reply&category="+bvo.getCategory());
		} else if (type.equals("basic")) {
			mv.setViewName("redirect:/board/view.do?head=" + bvo.getHead()
					+ "&type=basic&category="+bvo.getCategory());
		} else if (type.equals("answer")) {
			mv.setViewName("redirect:/board/view.do?head=" + bvo.getHead()
					+ "&type=answer&category="+bvo.getCategory());
		}

		return mv;
	}

	@RequestMapping("/board/updateInfo.do")
	public ModelAndView updateInfo(BoardVO bvo, HttpServletRequest request)
			throws Exception {
		ModelAndView mv = new ModelAndView();
		bvo = boardService.detailView(bvo);
		
		String type = request.getParameter("type");
		if (type.equals("reply")) {
			mv.addObject("bvo", bvo);
			mv.setViewName("reply/updateinfo.board");
		} else if (type.equals("basic")) {
			mv.addObject("bvo", bvo);
			mv.setViewName("basic/updateinfo.board");
		} else if (type.equals("answer")) {
			mv.addObject("bvo", bvo);
			mv.setViewName("answer/updateinfo.board");
		}

		return mv;
	}

	@RequestMapping("/board/writeInfo.do")
	public ModelAndView writeForm(BoardVO bvo, HttpServletRequest request) {
		ModelAndView mv = new ModelAndView();
		String type = request.getParameter("type");
		
		if (type.equals("reply")) {
			
			mv.addObject("bvo", bvo);
			mv.setViewName("reply/writeinfo.board");
		} else if (type.equals("basic")) {
			
			mv.addObject("bvo", bvo);
			mv.setViewName("basic/writeinfo.board");
		} else if (type.equals("answer")) {
			
			mv.addObject("bvo", bvo);
			mv.setViewName("answer/writeinfo.board");
		}

		return mv;
	}

	@RequestMapping("/board/update.do")
	public ModelAndView updateBoard(BoardVO bvo, HttpServletRequest request)
			throws Exception {
		ModelAndView mv = new ModelAndView();
		String type = request.getParameter("type");
		int countView = bvo.getCountView() - 1;

		boardService.updateBoard(bvo);
		if (type.equals("reply")) {
			mv.setViewName("redirect:/board/detail.do?type=reply&no="
					+ bvo.getNo() + "&countView=" + countView);
		} else if (type.equals("basic")) {
			mv.setViewName("redirect:/board/detail.do?type=basic&no="
					+ bvo.getNo() + "&countView=" + countView);
		} else if (type.equals("answer")) {
			mv.setViewName("redirect:/board/detail.do?type=answer&no="
					+ bvo.getNo() + "&countView=" + countView);
		}

		return mv;

	}

	@RequestMapping("/board/write.do")
	public ModelAndView wirte(BoardVO bvo, HttpServletRequest request)
			throws Exception {
		ModelAndView mv = new ModelAndView();
		String type = request.getParameter("type");
		String id = request.getParameter("id");
		
		Map map = new HashMap();
		
		map.put("id", id);
		map.put("category", bvo.getCategory());
		map.put("head", bvo.getHead());
		map.put("title", bvo.getTitle());
		map.put("content", bvo.getContent());
		 boardService.write(map);
		  int no =(int)map.get("board_no");
		//System.out.println(map.get("no"));
		
		if (type.equals("reply")) {
			mv.setViewName("redirect:/board/detail.do?type=reply&no=" + no);
		} else if (type.equals("basic")) {
			mv.setViewName("redirect:/board/detail.do?type=basic&no=" + no);
		} else if (type.equals("answer")) {
			mv.setViewName("redirect:/board/detail.do?type=answer&no=" +no);
		}
		return mv;
	}
	@RequestMapping("/board/answerInfo.do")
	public ModelAndView answerInfo(BoardVO bvo, HttpServletRequest request) throws Exception {
		ModelAndView mv = new ModelAndView();
		
		bvo = boardService.detailView(bvo);
		
		mv.addObject("bvo", bvo);
		mv.setViewName("answer/answerInfo.board");


		return mv;
	}
	@RequestMapping("/board/answerWrite.do")
	public ModelAndView answer(BoardVO bvo,HttpServletRequest request) throws Exception{
		ModelAndView mv=new ModelAndView();
		String id = request.getParameter("id");
		
			Map map = new HashMap();
			
			map.put("id", id);
			map.put("ref", bvo.getRef());
			map.put("restep", bvo.getRestep());
			map.put("no", bvo.getNo());
			
			map.put("category", bvo.getCategory());
			map.put("head", bvo.getHead());
			map.put("title", bvo.getTitle());
			map.put("content", bvo.getContent());
			map.put("relevel", bvo.getRelevel());
			
			boardService.answerWrite(map);
			int no =(int)map.get("answerno");
			mv.setViewName("redirect:/board/detail.do?type=answer&no=" +no);
		return mv;
	}
}
